package com.molyfun.parents.activity.store.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.coursecenter.view.ProductCatalogView;
import com.molyfun.parents.fragment.BaseFragment;
import com.molyfun.parents.rest.bean.ProductDetailInfoResponse;
import com.molyfun.parents.rest.bean.ProductatalogListResponse;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ProductDetailContentFragment extends BaseFragment {

    @InjectView(R.id.catalog_list)
    LinearLayout mCatalogList;
    @InjectView(R.id.course_type_count)
    TextView mCourseTypeCount;

    private ProductDetailInfoResponse.ProductDetailInfoBean mProductDetailBean;

    public static ProductDetailContentFragment newInstance(ProductDetailInfoResponse.ProductDetailInfoBean productDetailBean) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(Const.PRODUCT_DETAIL_BEAN, productDetailBean);
        ProductDetailContentFragment productDetailInfoFragment = new ProductDetailContentFragment();
        productDetailInfoFragment.setArguments(bundle);
        return productDetailInfoFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_product_detail_content, container, false);
        ButterKnife.inject(this, view);

        mProductDetailBean = getArguments().getParcelable(Const.PRODUCT_DETAIL_BEAN);

        if(mProductDetailBean != null){
            mCourseTypeCount.setText("直播课" + mProductDetailBean.getLivenum() + "节 " + "视频课" + mProductDetailBean.getMedianum() + "节 ");
        }

        getProductCatalog(mProductDetailBean.getId());

        return view;
    }

    private void getProductCatalog(String productId) {
        showProgressDialog();
        Call<ProductatalogListResponse> call = MFPApplication.getInstance().getRestClient().getProductCatalogList(productId);
        call.enqueue(new Callback<ProductatalogListResponse>() {
            @Override
            public void onResponse(Call<ProductatalogListResponse> call, Response<ProductatalogListResponse> response) {
                dismissProgressDialog();
                if (response.body() != null) {
                    if (response.body().getCode() == 200 && response.body().getData() != null) {
                        int position = 0;
                        mCatalogList.removeAllViews();
                        for(ProductatalogListResponse.ProductCatalogBrief productCatalogBrief : response.body().getData()){
                            ProductCatalogView productCatalogView = new ProductCatalogView(getContext());
                            productCatalogView.initData(productCatalogBrief, position == (response.body().getData().size() - 1), mProductDetailBean.isPurchasestatus(), mProductDetailBean.getId());
                            mCatalogList.addView(productCatalogView);
                            position ++;
                        }
                    } else {
                        toast(response.body().getMsg());
                    }
                } else {
                    toast("获取套餐详情失败");
                }
            }

            @Override
            public void onFailure(Call<ProductatalogListResponse> call, Throwable t) {
                dismissProgressDialog();
            }
        });
    }


}
